package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.PolicyDto;
import io.geekidea.boot.shop.entity.Policy;
import io.geekidea.boot.shop.mapper.PolicyMapper;
import io.geekidea.boot.shop.query.app.AppPolicyQuery;
import io.geekidea.boot.shop.query.web.PolicyQuery;
import io.geekidea.boot.shop.service.base.api.PolicyService;
import io.geekidea.boot.shop.vo.app.AppPolicyVo;
import io.geekidea.boot.shop.vo.web.PolicyVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addPolicy(PolicyDto dto) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(dto, policy);
        return save(policy);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePolicy(PolicyDto dto) {
        Integer id = dto.getId();
        Policy policy = getById(id);
        if (policy == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, policy);
        return updateById(policy);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePolicy(Integer id) {
        return removeById(id);
    }

    @Override
    public PolicyVo getPolicyById(Integer id) {
        return policyMapper.getPolicyById(id);
    }

    @Override
    public Paging<PolicyVo> getPolicyPage(PolicyQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<PolicyVo> list = policyMapper.getPolicyPage(query);
        Paging<PolicyVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppPolicyVo getAppPolicyById(Integer id) {
        return policyMapper.getAppPolicyById(id);
    }

    @Override
    public Paging<AppPolicyVo> getAppPolicyPage(AppPolicyQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppPolicyVo> list = policyMapper.getAppPolicyPage(query);
        Paging<AppPolicyVo> paging = new Paging<>(list);
        return paging;
    }

}
